package com.fuyao.cloud.asset.util;

import com.fuyao.cloud.asset.entity.AssetCard;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.util.Objects;

/**
 * @author ly
 * @date 2021/11/23 10:59
 */
public class AnnotationUtils {

	/**
	 * 根据字段名称获取apiModelProperty注解值。
	 *
	 * @param entityClass   实体对象class
	 * @param propertyClass apiModelPropery
	 * @param fieldName     字段名
	 * @return 字段名对应注解
	 */
	public static String getClassFieldAnnotationValue(Class<AssetCard> entityClass, Class<ApiModelProperty> propertyClass, String fieldName) {
		Field[] declaredFields = entityClass.getDeclaredFields();
		Objects.requireNonNull(entityClass);
		Objects.requireNonNull(fieldName);
		for (Field declaredField : declaredFields) {
			ApiModelProperty annotation = declaredField.getAnnotation(propertyClass);
			if (declaredField.getName().equals(fieldName)) {
				return annotation.value();
			}
		}
		return fieldName;
	}
}
